﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace ComicsEditor
{
    public partial class PageControl : UserControl
    {
        private PageInfo pageInfo;

        public PageControl()
        {
            InitializeComponent();
        }

        public void Bind(PageInfo pageInfo)
        {
            Visible = true;
            this.pageInfo = pageInfo;

            lblSize.Text = String.Format("{0}K", (pageInfo.Size + 1023)/1024);
            lblWidth.Text = pageInfo.Page != null ? pageInfo.Page.Width.ToString() : "-";
            lblHeight.Text = pageInfo.Page != null ? pageInfo.Page.Height.ToString() : "-";

            comboType.Text = pageInfo.Type;
            chkDouble.Checked = pageInfo.DoublePage;
            date.Checked = pageInfo.Date != "";
            if (date.Checked)
                date.Value = DateTime.Parse(pageInfo.Date);
            txtKey.Text = pageInfo.SaveKey != null ? pageInfo.SaveKey : pageInfo.Key;
            txtUrl.Text = pageInfo.URL;
        }

        private void comboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            pageInfo.Type = comboType.Text;
            (this.ParentForm as Form1).UpdateNames();
        }

        private void chkDouble_CheckedChanged(object sender, EventArgs e)
        {
            pageInfo.DoublePage = chkDouble.Checked;
        }

        private void date_ValueChanged(object sender, EventArgs e)
        {
            if (date.Checked)
                pageInfo.Date = date.Value.ToString("yyyy-MM-dd");
            else
                pageInfo.Date = "";
        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            pageInfo.SaveKey = txtKey.Text.Trim();
            if (pageInfo.Preview == null)
                pageInfo.ComicInfo.UpdatePreview(pageInfo);

        }

        private void txtUrl_TextChanged(object sender, EventArgs e)
        {
            pageInfo.URL = txtUrl.Text.Trim();
            if (pageInfo.Preview == null)
                pageInfo.ComicInfo.UpdatePreview(pageInfo);
        }
    }
}
